﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

public partial class GUI_Pages_BabysitterRegistration1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime myDateTime = DateTime.Now;
        string year = myDateTime.Year.ToString();
        int yr = int.Parse(year);

        ArrayList ayear = new ArrayList();
        int i;
        for (i = 1960; i <= yr; i++)
        {
            String n = i.ToString();
            ayear.Add(n);

        }
        byear.DataSource = ayear;
        byear.DataBind();

        ArrayList gradyear = new ArrayList();
        
        int j;
        for (j = 1960; j <= (yr + 10); j++)
        {
            String n = j.ToString();
            gradyear.Add(n);

        }

        sch1grad.DataSource = gradyear;
        sch1grad.DataBind();

        sch2grad.DataSource = gradyear;
        sch2grad.DataBind();

        uni1grad.DataSource = gradyear;
        uni1grad.DataBind();

        uni2grad.DataSource = gradyear;
        uni2grad.DataBind();
    }

    protected DateTime getBirthDate(String d, String m, String y)
    {
        Int32 day = Int32.Parse(d);
        Int32 month = Int32.Parse(m);
        Int32 year = Int32.Parse(y);
        DateTime dt = new DateTime(year, month, day);
        return dt;
    }

    protected void sittersform1_Click(object sender, EventArgs e)
    {
        string connStr = ConfigurationManager.ConnectionStrings["CSConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        SqlCommand cmd1 = new SqlCommand("AccountIDAssignment", conn);
        cmd1.CommandType = CommandType.StoredProcedure;

        cmd1.Parameters.Add(new SqlParameter("@acctID", 2));

        conn.Open();
        int accID = Convert.ToInt32((decimal)cmd1.ExecuteScalar());
        conn.Close();

        SqlCommand cmd2 = new SqlCommand("BabysittersRegistration1_Info", conn);
        cmd2.CommandType = CommandType.StoredProcedure;

        String name = bName.Text + " " + bName0.Text;

        cmd2.Parameters.Add(new SqlParameter("@full_name", name));
        cmd2.Parameters.Add(new SqlParameter("@acctID", accID));
        cmd2.Parameters.Add(new SqlParameter("@DoB", getBirthDate(bday.SelectedValue, bmonth.SelectedValue, byear.SelectedValue)));
        cmd2.Parameters.Add(new SqlParameter("@nationality_id", Int32.Parse(nationalitiesList.SelectedValue)));
        cmd2.Parameters.Add(new SqlParameter("@gender", Convert.ToChar(gender.SelectedValue)));
        cmd2.Parameters.Add(new SqlParameter("@cellNo", bMobno.Text));
        cmd2.Parameters.Add(new SqlParameter("@phoneNo", bphone.Text));
        cmd2.Parameters.Add(new SqlParameter("@email", bEmail.Text));
        cmd2.Parameters.Add(new SqlParameter("@address", baddress.Text));
        cmd2.Parameters.Add(new SqlParameter("@area", barea.Text));
        cmd2.Parameters.Add(new SqlParameter("@cityID", 1));
        cmd2.Parameters.Add(new SqlParameter("@postalCode", bpost.Text));

        conn.Open();
        int bsID = Convert.ToInt32((decimal)cmd2.ExecuteScalar());
        conn.Close();

        if (TextBox2.Text != "")
        {
            helper(bsID, TextBox2, DropDownList2, sch1grad, e);
        }
        if (TextBox3.Text != "")
        {
            helper(bsID, TextBox3, DropDownList4, sch2grad, e);
        }
        if (TextBox4.Text != "")
        {
            helper(bsID, TextBox4, DropDownList6, uni1grad, e);
        }
        if (TextBox5.Text != "")
        {
            helper(bsID, TextBox5, DropDownList8, uni2grad, e);
        }
        if (TextBox6.Text != "")
        {
            helper2(bsID, TextBox6, TextBox7, TextBox8, e);
        }
        if (TextBox9.Text != "")
        {
            helper2(bsID, TextBox9, TextBox10, TextBox11, e);
        }

       
        for (int k = 0; k < bLang.Items.Count; k++)
        {
            if (bLang.Items[k].Selected)
            {
                int x = int.Parse(bLang.Items[k].Value);
                SqlCommand cmd3 = new SqlCommand("BabySittersRegistration2_Languages", conn);
                cmd3.CommandType = CommandType.StoredProcedure;

                cmd3.Parameters.Add(new SqlParameter("@bsID", bsID));
                cmd3.Parameters.Add(new SqlParameter("@languageID", x));

                conn.Open();
                cmd3.ExecuteNonQuery();
                conn.Close();
            }
        }

        Response.Redirect("http://www.cairositters.com/GUI/Pages/BabysitterRegistration2.aspx?bsID="+bsID+"&bAcctID="+accID);
        //Response.Redirect("http://localhost:60070/CSWebSite_v1.0/GUI/Pages/BabysitterRegistration2.aspx?bsID=" + bsID + "&bAcctID=" + accID);
    }


    protected void helper(int b, TextBox t, DropDownList d1, DropDownList d2, EventArgs e)
    {
        string connStr = ConfigurationManager.ConnectionStrings["CSConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        SqlCommand cmd1 = new SqlCommand("SchoolUniversity", conn);
        cmd1.CommandType = CommandType.StoredProcedure;

        cmd1.Parameters.Add(new SqlParameter("@name", t.Text));

        conn.Open();
        int suID = Convert.ToInt32((decimal)cmd1.ExecuteScalar());
        conn.Close();

        SqlCommand cmd2 = new SqlCommand("BabySittersRegistration1_Edu", conn);
        cmd2.CommandType = CommandType.StoredProcedure;

        cmd2.Parameters.Add(new SqlParameter("@bsID", b));
        cmd2.Parameters.Add(new SqlParameter("@suID", suID));
        cmd2.Parameters.Add(new SqlParameter("@years_completed", Convert.ToInt32(d1.SelectedValue)));
        cmd2.Parameters.Add(new SqlParameter("@grad_year", Convert.ToInt32(d2.SelectedValue)));

        conn.Open();
        cmd2.ExecuteNonQuery();
        conn.Close();
    }

    protected void helper2(int b, TextBox t1, TextBox t2, TextBox t3, EventArgs e)
    {
        string connStr = ConfigurationManager.ConnectionStrings["CSConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);

        SqlCommand cmd1 = new SqlCommand("BSEmployers", conn);
        cmd1.CommandType = CommandType.StoredProcedure;

        cmd1.Parameters.Add(new SqlParameter("@e_name", t1.Text));

        conn.Open();
        int empID = Convert.ToInt32((decimal)cmd1.ExecuteScalar());
        conn.Close();

        SqlCommand cmd2 = new SqlCommand("BabySittersRegistration1_Emp", conn);
        cmd2.CommandType = CommandType.StoredProcedure;

        cmd2.Parameters.Add(new SqlParameter("@bsID", b));
        cmd2.Parameters.Add(new SqlParameter("@empID", empID));
        cmd2.Parameters.Add(new SqlParameter("@date", t2.Text));
        cmd2.Parameters.Add(new SqlParameter("@duties", t3.Text));

        conn.Open();
        cmd2.ExecuteNonQuery();
        conn.Close();
    }


    
}